﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace Timetable.AWP.ViewModels
{
    internal class Schedule: BaseViewModel
    {
        public IList<ScheduleRecord> Records { get; set; }

        #region Loaded groups schedule

        public static readonly DependencyProperty GroupSchedulesProperty = DependencyProperty.RegisterAttached(
                    "GroupsSchedules",
                    typeof(ObservableCollection<Host.Schedule.Schedule>),
                    typeof(Schedule),
                    new PropertyMetadata(
                        new ObservableCollection<Host.Schedule.Schedule>(), 
                        OnGroupSchedulesChanged));

        public ObservableCollection<Host.Schedule.Schedule> GroupSchedules
        {
            get { return GetValue(GroupSchedulesProperty) as ObservableCollection<Host.Schedule.Schedule>; }
            set
            {
                var oldValue = GetValue(GroupSchedulesProperty);
                SetValue(GroupSchedulesProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(GroupSchedulesProperty, oldValue, value));
            }
        }

        //public static readonly RoutedEvent GroupsScheduleChangedEvent =
        //    EventManager.RegisterRoutedEvent("GroupsScheduleChanged",
        //                                     RoutingStrategy.Bubble,
        //                                     typeof(RoutedPropertyChangedEventHandler<ObservableCollection<Host.Schedule.Schedule>>),
        //                                     typeof(Schedule));

        private static void OnGroupSchedulesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

        #region Loaded specialities schedule

        public static readonly DependencyProperty SpecialitiesScheduleProperty = DependencyProperty.RegisterAttached(
                                    "SpecialitiesSchedule",
                                    typeof(ObservableCollection<Host.Schedule.Schedule>),
                                    typeof(Schedule),
                                    new FrameworkPropertyMetadata(new ObservableCollection<Host.Schedule.Schedule>(), OnSpecialitySchedulesChanged));
        public ObservableCollection<Host.Schedule.Schedule> SpecialitiesSchedule
        {
            get { return GetValue(SpecialitiesScheduleProperty) as ObservableCollection<Host.Schedule.Schedule>; }
            set
            {
                var oldValue = GetValue(SpecialitiesScheduleProperty);
                SetValue(SpecialitiesScheduleProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(SpecialitiesScheduleProperty, oldValue, value));
            }
        }

        private static void OnSpecialitySchedulesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

        #region Loaded course schedule
        public static readonly DependencyProperty CourseSchedulesProperty = DependencyProperty.RegisterAttached(
                                    "CourseSchedules",
                                    typeof(ObservableCollection<Host.Schedule.Schedule>),
                                    typeof(Schedule),
                                    new FrameworkPropertyMetadata(new ObservableCollection<Host.Schedule.Schedule>(), OnCourseSchedulesChanged));
        public Host.Schedule.Schedule CourseSchedules
        {
            get { return GetValue(CourseSchedulesProperty) as Host.Schedule.Schedule; }
            set
            {
                var oldValue = GetValue(CourseSchedulesProperty);
                SetValue(CourseSchedulesProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(CourseSchedulesProperty, oldValue, value));
            }
        }
        
        private static void OnCourseSchedulesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

        public Schedule()
        {
            Records = new List<ScheduleRecord>();
            var times = TimeService.GetAll();

            foreach (var time in times)
            {
                Records.Add(new ScheduleRecord
                                {
                                    StartTime = time.Start,
                                    EndTime = time.End
                                });
            }
        }
    }
}
